package com.fang.networkdisk.controller;

import com.fang.common.common.CommonPage;
import com.fang.common.common.CommonResult;
import com.fang.common.request.BaseController;
import com.fang.common.util.CommonResultUtil;
import com.fang.networkdisk.entity.bo.RoleBo;
import com.fang.networkdisk.entity.dto.RoleDto;
import com.fang.networkdisk.entity.vo.RoleVo;
import com.fang.networkdisk.service.TbNetDisRoleService;
import com.fang.networkdisk.util.NDBeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description
 * @Author Bernie_fang
 * @Date 2025/2/26 22:32
 **/
@RestController
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    @Resource
    private TbNetDisRoleService tbNetDisRoleService;
    
    @PostMapping("/list")
    public CommonResult<CommonPage<List<RoleVo>>> queryRole(@RequestBody RoleDto roleDto) {
        RoleBo roleBo = new RoleBo();
        NDBeanUtils.copyListProperties(roleDto, roleBo);
        CommonPage result = tbNetDisRoleService.queryRole(roleBo);
        return CommonResultUtil.success(result);
    }

    @PostMapping("/add")
    public CommonResult<CommonPage<List<RoleVo>>> addRole(@RequestBody RoleDto roleDto) {
        String authorId = super.getAuthorId();
        RoleBo roleBo = new RoleBo();
        NDBeanUtils.copyListProperties(roleDto, roleBo);
        roleBo.setCreateUser(authorId);
        roleBo.setUpdateUser(authorId);
        tbNetDisRoleService.addRole(roleBo);
        return CommonResultUtil.success();
    }

    @PostMapping("/update")
    public CommonResult<CommonPage<List<RoleVo>>> updateRole(@RequestBody RoleDto roleDto) {
        String authorId = super.getAuthorId();
        RoleBo roleBo = new RoleBo();
        NDBeanUtils.copyListProperties(roleDto, roleBo);
        roleBo.setIdentifier(null);
        roleBo.setCreateUser(null);
        roleBo.setUpdateUser(authorId);
        tbNetDisRoleService.updateRoleById(roleBo);
        return CommonResultUtil.success();
    }

    @GetMapping("/delete/{id}")
    public CommonResult<CommonPage<List<RoleVo>>> deleteRole(@PathVariable("id") Integer id) {
        String authorId = super.getAuthorId();
        tbNetDisRoleService.deleteRoleById(id, authorId);
        return CommonResultUtil.success();
    }
    
}
